<?php

namespace app\common\model;

use think\Env;
use think\Model;

/**
 * 模型
 */
class Recharge extends Model
{


    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    const STATUS_AGENT_RECHARGE = 0;   // 等待代理商充值
    const STATUS_PLATFORM_RECHARGE = 1;   // 等待平台充值
    const STATUS_CONFIRM = 2; // 等待商家确认
    const STATUS_COMPLETE= 3; // 充值完成



    const STATUS_OK = 1;
    const STATUS_NO = 0;
    static $statusMap = [
        self::STATUS_OK => '已充值',
        self::STATUS_NO => '未处理',
    ];

    public function shop()
    {
        return $this->belongsTo('shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function agents()
    {
        return $this->belongsTo('agent', 'agent_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function agent()
    {
        return $this->belongsTo(Agent::class, 'agent_id');
    }

    public function getAgentRecordAttr($value)
    {
        return !empty($value) ? Env::get('app.app_host') . $value : '';
    }

    public function getPlatformRecordAttr($value)
    {
        return !empty($value) ? Env::get('app.app_host') . $value : '';
    }

    /**
     * 商家充值记录
     * @param $shopId
     * @param \Closure|null $closure
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function shopRechargeRecord($shopId, ?\Closure $closure = null)
    {
        return self::where('shop_id', $shopId)->where(function ($query) use ($closure) {
            if ($closure instanceof \Closure) {
                $closure($query);
            }})->with(['agent'])->order('createtime desc')->paginate();
    }

}
